// needed for built in API call
import java.io.PrintStream;
import java.net.Socket;

//needed for mail API call
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;

public class library {
    
    public String sendUsingBuiltInAPI(String host, String to, String from, String subject, String message){
        
        String results = "";
            
        // stores all recipients in an array
        String[] rec = to.split(";");

        // extract the domain of the from address
        // yourname@example.com produces example.com
        String domain = from.substring(from.indexOf('@') + 1);

        for (int i = 0; i < rec.length; i++) {

            try {

                // declare server and port
                Socket s = new Socket(host, 25);

                // create a print stream
                PrintStream out = new PrintStream(s.getOutputStream());

                out.print("EHLO " + domain + "\r\n");
                out.print("MAIL FROM: <" + from + ">\r\n");
                out.print("RCPT TO: <" + rec[i].trim() + ">\r\n");
                out.print("DATA\r\n");
                out.print("From: <" + from + ">\r\n");
                out.print("To: <" + rec[i].trim() + ">\r\n");
                out.print("Subject: " + subject + "\r\n");
                out.print(message + "\r\n");
                out.print(".\r\n");

                results += rec[i].trim() + " sent\n";

                s.close();

            } catch (Exception ex) {

                results += rec[i].trim() + " error - not sent\n";
            }

        } // end for-loop
        
        return results;

    } // end send
    
    public void sendUsingMailAPI(String host, String to, String from, String subject, String content, String month, String day, String year, String hour, String minute){
        
        Date d = new Date();
        
        try{
            int mo = Integer.parseInt(month)-1;
            int da = Integer.parseInt(day);
            int y = Integer.parseInt(year)-1900;
            int h = Integer.parseInt(hour);
            int mi = Integer.parseInt(minute);
            
            d.setMonth(mo);
            d.setDate(da);
            d.setYear(y);
            d.setHours(h);
            d.setMinutes(mi);
            
        }
        catch(Exception e){
            
            d = new Date(); // if there was an error, set date to current time
            System.out.println(d.getDay() + " " + d.getMonth());
        }

        try{
            System.out.println(from);
            // Get system properties
            Properties properties = System.getProperties();

            // Setup mail server
            properties.setProperty("mail.smtp.host", host);

            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties);

            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set the RFC 822 "From" header field using the
            // value of the InternetAddress.getLocalAddress method.
            message.setFrom(new InternetAddress(from));

            String[] rec = to.split(";");

            // runs through list of all recipients and adds them to the email as BCC
            // so no person can see the other people who were emailed
            for(int i=0; i < rec.length; i++){

                // Add the given addresses to the specified recipient type.
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(rec[i].trim()));
            }

            // Set the "Subject" header field.
            message.setSubject(subject);
            
            // set email date
            message.setSentDate(d);

            // Sets the given String as this part's content,
            // with a MIME type of "text/plain".
            message.setText(content);

            // Send message
            Transport.send(message);

            System.out.println("Message sent");
        }
        catch(Exception e){
            System.out.println("Some error has occurred.\n" + e);
        }
        
    } // end sendUsingMailAPI
    
} // end library